<template>
  <view class="waterfall-container">
    <view class="waterfall-column">
      <view 
        v-for="(item, index) in leftColumn" 
        :key="index"
        class="waterfall-item"
        @click="handleItemClick(item)"
      >
        <image 
          :src="item.mainImage" 
          mode="widthFix" 
          class="product-image"
          @load="imageLoad"
        />
        <view class="product-info">
          <text class="product-name">{{ item.name }}</text>
          <text class="product-subtitle" v-if="item.description">{{ item.description }}</text>
          <view class="price-row">
            <text class="price">¥{{ item.price }}</text>
            <text class="original-price" v-if="item.originalPrice">¥{{ item.originalPrice }}</text>
          </view>
          <view class="extra-info">
            <text class="sales" v-if="item.sales">销量 {{ item.sales }}</text>
            <view class="tags">
              <text class="tag hot" v-if="item.isHot">热销</text>
              <text class="tag new" v-if="item.isNew">新品</text>
              <text class="tag recommend" v-if="item.isRecommend">推荐</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    
    <view class="waterfall-column">
      <view 
        v-for="(item, index) in rightColumn" 
        :key="index"
        class="waterfall-item"
        @click="handleItemClick(item)"
      >
        <image 
          :src="item.mainImage" 
          mode="widthFix" 
          class="product-image"
          @load="imageLoad"
        />
        <view class="product-info">
          <text class="product-name">{{ item.name }}</text>
          <text class="product-subtitle" v-if="item.description">{{ item.description }}</text>
          <view class="price-row">
            <text class="price">¥{{ item.price }}</text>
            <text class="original-price" v-if="item.originalPrice">¥{{ item.originalPrice }}</text>
          </view>
          <view class="extra-info">
            <text class="sales" v-if="item.sales">销量 {{ item.sales }}</text>
            <view class="tags">
              <text class="tag hot" v-if="item.isHot">热销</text>
              <text class="tag new" v-if="item.isNew">新品</text>
              <text class="tag recommend" v-if="item.isRecommend">推荐</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'WaterfallFlow',
  props: {
    products: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      leftColumn: [],
      rightColumn: [],
      loadedImages: 0
    }
  },
  watch: {
    products: {
      immediate: true,
      handler(newProducts) {
        this.distributeProducts(newProducts)
        this.loadedImages = 0
      }
    }
  },
  methods: {
    distributeProducts(products) {
      this.leftColumn = products.filter((_, index) => index % 2 === 0)
      this.rightColumn = products.filter((_, index) => index % 2 === 1)
    },
    handleItemClick(item) {
      this.$emit('item-click', item)
    },
    imageLoad() {
      this.loadedImages++
      if (this.loadedImages === this.products.length) {
        this.$emit('all-images-loaded')
      }
    },
    handleImageError(e) {
      console.error('Image load error:', e)
    }
  }
}
</script>

<style lang="scss" scoped>
.waterfall-container {
  display: flex;
  padding: 16rpx;
  box-sizing: border-box;
  background-color: #ffffff;
}

.waterfall-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 8rpx;
}

.waterfall-item {
  background-color: #fff;
  border-radius: 12rpx;
  margin-bottom: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.product-image {
  width: 100%;
  height: auto;
  display: block;
  background-color: #f5f5f5;
  min-height: 200rpx;
}

.product-info {
  padding: 16rpx;
}

.product-name {
  font-size: 28rpx;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  margin-bottom: 12rpx;
}

.product-subtitle {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 8rpx;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.price-row {
  display: flex;
  align-items: center;
}

.price {
  font-size: 32rpx;
  color: #ff4444;
  font-weight: bold;
  margin-right: 12rpx;
}

.original-price {
  font-size: 24rpx;
  color: #999;
  text-decoration: line-through;
}

.extra-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8rpx;
  
  .sales {
    font-size: 24rpx;
    color: #999;
  }
  
  .tags {
    display: flex;
    gap: 8rpx;
    
    .tag {
      font-size: 20rpx;
      padding: 2rpx 8rpx;
      border-radius: 4rpx;
      
      &.hot {
        color: #ff4444;
        background-color: #fff0f0;
      }
      
      &.new {
        color: #ff9500;
        background-color: #fff9f0;
      }
      
      &.recommend {
        color: #07c160;
        background-color: #f0fff0;
      }
    }
  }
}
</style> 